<template>
  <el-dialog v-el-drag-dialog title="版本管理" :visible.sync="visible" :append-to-body="appendToBody">
    <el-row>
      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-table
          ref="publish_table"
          v-loading="tableLoading"
          :data="tableData"
          height="350px"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
        >
          <el-table-column align="center" label="流程标识" prop="processKey" width="150" show-overflow-tooltip />
          <el-table-column align="center" label="流程名称" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" @click="handleProcessView(scope.row.definitionId)">
                <span>{{ scope.row.processName }}</span>
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="流程版本" width="90" align="center">
            <template slot-scope="scope">
              <el-tag size="medium" >v{{ scope.row.version }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="流程状态" width="85">
            <template slot-scope="scope">
              <el-tag type="success" v-if="!scope.row.suspended">激活</el-tag>
              <el-tag type="warning" v-if="scope.row.suspended">挂起</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="80" fixed="right">
            <template slot-scope="scope">
              <el-dropdown>
                <span class="el-dropdown-link">
                  <i class="el-icon-s-operation"/><i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-permission="['flowable:deploy:status']">
                    <el-link type="warning" icon="el-icon-video-pause" @click="handleChangeState(scope.row.definitionId, 'suspended')">挂起</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['flowable:deploy:status']">
                    <el-link type="success" icon="el-icon-video-play" @click="handleChangeState(scope.row.definitionId, 'active')">激活</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['flowable:deploy:delete']">
                    <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.deploymentId)">删除</el-link>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData(false)" />
      </el-card>
    </el-row>

    <!-- 流程图 -->
    <el-drawer :title="processView.title" :visible.sync="processView.open" direction="rtl" :modal="true" size="60%" append-to-body>
      <process-viewer :key="`designer-${processView.index}`" :xml="processView.xmlData" :style="{height: '500px'}" />
    </el-drawer>
  </el-dialog>
</template>

<script>
import { definitionPublishTable, definitionXml, definitionChangeState, definitionDelete } from '@/api/tool/workflow'
import ProcessViewer from '@/components/ProcessViewer'

export default {
  name: 'DefinitionPublishDialog',
  components: {
    ProcessViewer
  },
  props: {
    appendToBody: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      visible: false,
      queryData: {
        processKey: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      processView: {
        title: '',
        open: false,
        index: undefined,
        xmlData: '',
      }
    }
  },
  methods: {
    init(processKey) {
      this.visible = true
      this.queryData.processKey = processKey
      this.$nextTick(() => {
        this.getTableData()
      })
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    getTableData() {
      this.tableLoading = true
      const query = Object.assign(this.tableQuery, this.queryData)
      definitionPublishTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.publish_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    handleProcessView(definitionId) {
      this.processView.title = '流程图'
      this.processView.index = definitionId
      definitionXml(definitionId).then(response => {
        this.processView.xmlData = response.param
      })
      this.processView.open = true
    },
    handleChangeState(definitionId, state) {
      this.tableLoading = true
      definitionChangeState({
          definitionId: definitionId,
          state: state
      }).then(response => {
        this.$modal.msgSuccess(response.msg)
        this.getTableData()
        this.$emit('refresh', null)
      }).finally(() => this.tableLoading = false)
    },
    handleDelete(deploymentId) {
      this.$confirm('是否删除所选流程部署?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        definitionDelete({ ids: deploymentId }).then(response => {
          this.$modal.msgSuccess('删除成功!')
          this.getTableData()
          this.$emit('refresh', null)
        })
      })
    }
  }
}
</script>
